<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow = true" type="primary" round plain
        >新增</el-button
      >
    </div>
    <el-table :data="bannerList">
      <el-table-column prop="id" label="序号" min-width="80"></el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        min-width="180"
      ></el-table-column>
      <el-table-column prop="img" label="图片" min-width="180">
        <template slot-scope="props">
          <img :src="$baseUrl + props.row.img" style="height: 80px" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template slot-scope="props">
          <el-button
            type="success"
            size="mini"
            plain
            round
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button
            type="danger"
            size="mini"
            plain
            round
            v-if="props.row.status === 2"
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button
            @click="edit(props.row)"
            type="success"
            size="mini"
            round
            plain
            >编辑</el-button
          >
          <el-button
            @click="del(props.row.id)"
            type="danger"
            size="mini"
            round
            plain
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="size"
      :total="bannerTotal"
      layout="prev,pager,next"
      @current-change="pageChange"
    ></el-pagination>
    <!-- 调用子组件 -->
    <Dialog ref="dialog" @updateBanner="updateBannerHandle" :info="info" />
  </div>
</template>

<script>
// 导入对画框组件
import Dialog from "./Dialog";
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  components: { Dialog },
  data() {
    return {
      info: {
        isShow: false,
      },
      page: 1,
      size: 2,
    };
  },
  computed: {
    ...mapState("banner", ["bannerList", "bannerTotal"]),
  },
  methods: {
    ...mapActions("banner", ["getBannerListAction", "getBannerTotalAction"]),
    // 删除按钮点击事件处理函数
    del(id) {
      this.$myConfirm(() => {
        this.$axios.post("/bannerdelete", { id }).then((res) => {
          if (res.code === 200) {
            this.$message({ type: "success", message: res.msg });
            this.updateBannerHandle();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        });
      });
    },
    // 编辑按钮点击事件处理函数
    edit(row) {
      // 1-显示对话框组件
      this.info.isShow = true;
      // 2-将待编辑的活动信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    },
    // 分页按钮点击事件处理函数
    pageChange(page) {
      // 更新页码
      this.page = page;
      // 重新请求数据列表
      this.getBannerListAction({ page: this.page, size: this.size });
    },
    // 自定义事件处理函数
    updateBannerHandle() {
      // 请求活动列表
      this.getBannerListAction({ page: this.page, size: this.size });
      // 请求活动总数
      this.getBannerTotalAction();
    },
  },
  created() {
    this.updateBannerHandle();
  },
};
</script>

<style>
</style>